package net.seabookchen.controller;

import java.util.ArrayList;
import java.util.List;

import org.datanucleus.util.StringUtils;
import org.springframework.ui.Model;

public abstract class CommonController<T> {
	public void populateModel(Model model, T entity, String message) {
		List<T> entities = new ArrayList<T>();
		entities.add(entity);
		model.addAttribute("action", message);
		model.addAttribute("entities", entities);
		
		// For support JAXB;
		String entityName = getClassName(entity.getClass().getName());
		model.addAttribute(entityName, entity);
	}
	
	private String getClassName(String fullname) {
		if (StringUtils.isEmpty(fullname)) {
			throw new IllegalArgumentException("Class Name can't be empty!");
		}
		
		int index = fullname.lastIndexOf(".");
		return fullname.substring(index);
		
	}
}
